﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using System.Data.SqlClient;

namespace luanvanFinal
{
    /// <summary>
    /// Interaction logic for wdProjectCreate.xaml
    /// </summary>
    public partial class wdProjectCreate : Window
    {
        public wdProjectCreate()
        {
            InitializeComponent();
        }
        public Boolean checkProjectName(String name)
        {
            Boolean result = false;
            clsProject project = new clsProject();
            DataTable dt = project.getProjectName();
            //String [] proName = new String();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (name == dt.Rows[i][0].ToString())
                    result = true;

            }
            return result;
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            clsProject project = new clsProject();
            Boolean checkprojectname = checkProjectName(TbProjectName.Text);
            project.ProName = TbProjectName.Text;
            project.ProPM = TbPMName.Text;
            project.ProCustomer = TbCosName.Text;
            project.ProStartDay = dpStartDay.SelectedDate;
            project.ProEndDay = dpEndDay.SelectedDate;
            project.ProNote = new TextRange(tbNote.Document.ContentStart, tbNote.Document.ContentEnd).Text;
            if (checkprojectname == true)
                MessageBox.Show("Project name has existed in database", "Warrning", MessageBoxButton.OK, MessageBoxImage.Stop);
            else if (project.ProName == "")
                MessageBox.Show("Project name can not null !!!", "Warrning", MessageBoxButton.OK, MessageBoxImage.Stop);
            else if (project.ProEndDay <= project.ProStartDay)
            {
                MessageBox.Show("End Day must be greater Start Day !!!", "Warrning", MessageBoxButton.OK, MessageBoxImage.Stop);

            }
            else if (project.ProStartDay == null)
                MessageBox.Show("Start Day can not null !!!", "Warrning", MessageBoxButton.OK, MessageBoxImage.Stop);
            else if (project.ProEndDay == null)
                MessageBox.Show("End Day can not null !!!", "Warrning", MessageBoxButton.OK, MessageBoxImage.Stop);
            else
            {
                project.insertProject();
                MessageBox.Show("You have insert success !!!", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            String strSQL = "select MAX(ID) from project";
            clsConnect ketnoi = new clsConnect();
            ketnoi.openConnection();
            SqlDataAdapter da = new SqlDataAdapter(strSQL, ketnoi.Connection);
            DataTable dt = new DataTable();
            da.Fill(dt);
            ketnoi.closeConnection();
            try
            {
                clsProgress progress = new clsProgress();
                progress.IdProject = Convert.ToInt32(dt.Rows[0][0].ToString());
                progress.insertProgress();
            }
            catch
            {
            }
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}
